<?php

// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 生产查询
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   create by allen.qiang
 */

quanli_require('module/base/bill/billSearch.class.php');

class pa_productionSearch {

    public function execute() {

        global $G_PGLOB;

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']) {

                case 1: //显示列表页面
                    $this->productionSearchList();
                    break;

                case 2: //导出数据
                    $this->exportMessageList();
                    break;

                default:
                    $G_PGLOB->smarty->assign('msg', '参数错误，请审核后再操作');
                    $G_PGLOB->smarty->display('comm/error.tpl');
            }
        }
    }

    /**
     * 显示列表页面
     */
    private function productionSearchList() {
        global $G_PGLOB;
        //当前时间
        $currentDate = date('Y-m-d');
        $city = get_city();
//        echo '<pre>';
//        print_r($city);exit;
        $G_PGLOB->smarty->assign('currentDate',$currentDate);
        $G_PGLOB->smarty->assign('city',$city);//查询城市
        $G_PGLOB->smarty->display('production/productionSearch.tpl');


    }

    /**
     * 导出Excel
     */
    private function exportMessageList() {

        global $G_PGLOB;

        //获得导出数据
        $stat_array = $this->searchProduction();

        $stat_array = $stat_array['stat_array'];

        //检查phpexcel类
        quanli_include('framework/phpExecl/PHPExcel.php');
        quanli_include('framework/phpExecl/PHPExcel/Writer/Excel2007.php');

        //实例excel类
        $objPHPExcel = new PHPExcel();

        /**
         * 设置excel的属性
         */
        //创建人
        $objPHPExcel->getProperties()->setCreator("$G_PGLOB->currentUserName");
        //最后修改人
        $objPHPExcel->getProperties()->setLastModifiedBy("$G_PGLOB->currentUserName");
        //标题
        $objPHPExcel->getProperties()->setTitle('生产商品信息');
        //题目
        $objPHPExcel->getProperties()->setSubject('生产商品信息');
        //描述
        $objPHPExcel->getProperties()->setDescription('生产商品信息');

        /**
         * 设置excel的属性
         */

        /**
         * sheet
         */
        //设置当前的sheet
        $objPHPExcel->setActiveSheetIndex(0);
        //设置sheet的name
        $objPHPExcel->getActiveSheet()->setTitle('生产商品信息');
        /**
         * sheet
         */

        /**
         * 设置标题
         */

        $objPHPExcel->getActiveSheet()->setCellValue('A1', '订单号');
        $objPHPExcel->getActiveSheet()->setCellValue('B1', '商品名称');
        $objPHPExcel->getActiveSheet()->setCellValue('C1', '商品规格');
        $objPHPExcel->getActiveSheet()->setCellValue('D1', '商品数量');

        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);

        /**
         * 设置标题
         */

        /**
         * 添加数据
         */
        if ($stat_array) {

            $i = 2;

            foreach($stat_array as &$stat) {

                    $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $stat['bill_no'] . "\t");
                    $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $stat['goodsName']);
                    $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $stat['spec']);
                    $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $stat['goods_amount']);

                    $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                    $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
                    $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
                    $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);

                    $i++;
            }

        }
        /**
         * 添加数据
         */

        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(35);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(45);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(45);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);


        //文件名称
        $fileName = '生产商品信息_' . date('YmdHis') . '.xlsx';

        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        header("Content-Type: application/force-download");
        header("Content-Disposition:inline;filename=$fileName");
        header("Content-Transfer-Encoding: binary");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        $objWriter->save('php://output');

    }

    /**
     * 获得导出数据
     */
    private function searchProduction() {

        global $G_PGLOB;

        //返回数组
        $return_array = array(
            'production_array' => array(),      //统计时间段内的会员信息
        );


        $bill_goods_array = array(); //订单商品数组

        //实例订单主表类
        $billMaster = new billSearch($G_PGLOB->DBCA);

        $_REQUEST['bill_status'] = 1; //查询确认状态订单

        //查找订单主表数据
        $bill_array = $billMaster->searchBillInfo( $_REQUEST ) ;


        if($bill_array){
            foreach($bill_array as &$master){
                $arg_array = array(
                    'bill_no' => $master['bill_no']
                );
                //查找订单goods表数据
                $temp_goods_array = $billMaster->searchBillGoods( $arg_array ) ;

                foreach($temp_goods_array as &$goods) {
                    $bill_goods_array[] = $goods;
                }

            }
        }


        if ($bill_goods_array) {
            foreach ($bill_goods_array as &$goodsInfo) {

                $return_array['stat_array'][] = array(
                        'bill_no'=> $goodsInfo['bill_no'],
                        'goodsName'=> $goodsInfo['goodsName'],
                        'spec'=> $goodsInfo['spec'],
                        'goods_amount'=> $goodsInfo['goods_amount']
                    );
            }
        }

        return $return_array;

    }
}
